#include "GxWinAccel.h"
#include "GxWinMenu.h"

namespace GxWinGUI
{
	Accelerator::Accelerator()
	{
		memset(&accel, 0, sizeof(ACCEL));
	}

	Accelerator::Accelerator(AccelAuxKey auxKey, Word mainKey)
	{
		accel.fVirt = auxKey + FVIRTKEY;
		accel.key = mainKey;
	}

	AccelTable::AccelTable()
	{
		handle = 0;
	}

	AccelTable::~AccelTable()
	{
		if (handle)
			DestroyAcceleratorTable(handle);
	}

	HACCEL AccelTable::GetHandle()
	{
		return handle;
	}

	void AccelTable::RegisterAccel(Accelerator acc, MenuItem * item)
	{
		bool find = false;
		for (int i=0; i<accels.Count(); i++)
		{
			if (accels[i].fVirt == acc.accel.fVirt && accels[i].key == acc.accel.key)
			{
				find = true;
				break;
			}
		}
		if (find)
		{
			throw L"Accelerator confliction.";
		}
		acc.accel.cmd = item->GetIdentifier();
		accels.Add(acc.accel);
	}

	void AccelTable::Update()
	{
		if (handle)
			DestroyAcceleratorTable(handle);
		handle = CreateAcceleratorTable(accels.Buffer(), accels.Count());
	}
}